#include "StdAfx.h"
#include "RenderEngine.h"
#include "GraphicDevice.h"
#include "AnyDocument.h"
#include "ShapeReps.h"
#include "SelectionSet.h"
#include "AnyView.h"
#include "SketchPlane.h"
#include <TDF_Tool.hxx>
#include <Aspect_Grid.hxx> 
#include <TPrsStd_AISViewer.hxx>
#include <V3d_RectangularGrid.hxx>
CRenderEngine::CRenderEngine(void)
{
	Handle(Graphic3d_WNTGraphicDevice) hDC = CGraphicDevice::GetGraphicDC();

	TCollection_ExtendedString strName("AnyViewer");
	V3d_Viewer* pViewer = new V3d_Viewer(hDC,strName.ToExtString());
	m_hViewMgr = Handle_V3d_Viewer(pViewer);
	m_hContext = new AIS_InteractiveContext(m_hViewMgr);
}

CRenderEngine::~CRenderEngine(void)
{
}

void CRenderEngine::AttachToLabel(TDF_Label& hLabel)
{
	TPrsStd_AISViewer::New(hLabel,m_hViewMgr);
	TPrsStd_AISViewer::Find(hLabel, m_hContext);
	m_hContext->SetDisplayMode(AIS_Shaded);
	m_hContext->SelectionColor(Quantity_NOC_GREEN);
}

Handle_V3d_View CRenderEngine::AttachToWindow(HWND hWnd)
{
	Handle(Graphic3d_WNTGraphicDevice) hDC = CGraphicDevice::GetGraphicDC();
	Handle(WNT_Window) hWindow = new WNT_Window(hDC,hWnd);
	if(!hWindow->IsMapped())
		hWindow->Map();

	Standard_Integer nWidth = 100, nHeight = 100;
	hWindow->Size(nWidth,nHeight);

	Handle_V3d_View hView = m_hViewMgr->CreateView();
	hView->SetDegenerateModeOn();
	hView->SetWindow(hWindow);
	hView->SetBackgroundColor(Quantity_TOC_RGB,191.0/255.0,219.0/255.0,255.0/255.0);

	hView->SetProj(V3d_Zpos);

	// light
	m_hViewMgr->SetDefaultLights();
	m_hViewMgr->SetLightOn();

	// grid
	m_hViewMgr->ActivateGrid(Aspect_GT_Rectangular,Aspect_GDM_Lines);
	Handle_Aspect_Grid grid = m_hViewMgr->Grid();
	grid->SetColors(Quantity_NOC_ANTIQUEWHITE, Quantity_NOC_WHITE);

	return hView;
}

void CRenderEngine::Render(const CShapeReps& shape, bool bUpdate)
{
	Handle(AIS_Shape) hAisShape = shape.GetAisShape();
	if(!hAisShape.IsNull())
	{
		m_hContext->Display(hAisShape, bUpdate);
	}
}

void CRenderEngine::Remove(const CShapeReps& shape, bool bUpdate)
{

	Handle(AIS_Shape) hAisShape = shape.GetAisShape();
	if(!hAisShape.IsNull())
	{
		m_hContext->Remove(hAisShape, bUpdate);
	}

}

void CRenderEngine::SetColor(const CShapeReps& shape, 
							 COLOR_INDEX nColor, bool bUpdate)
{
	Handle(AIS_Shape) hAisShape = shape.GetAisShape();
	if(!hAisShape.IsNull())
	{
		m_hContext->SetColor(hAisShape,nColor,bUpdate);
	}

}

void CRenderEngine::SetTransparency(const CShapeReps& shape, 
							 double dValue, bool bUpdate)
{
	Handle(AIS_Shape) hAisShape = shape.GetAisShape();
	if(!hAisShape.IsNull())
	{
		m_hContext->SetTransparency(hAisShape,dValue,bUpdate);
	}

}

void CRenderEngine::UpdateCurrentViewer()
{
	m_hContext->UpdateCurrentViewer();
}

void CRenderEngine::UpdateAllViews()
{

	m_hViewMgr->Redraw();
}
void CRenderEngine::Hilight(const CShapeReps& shape, bool bUpdate /*= true*/)
{
	Handle(AIS_Shape) hAisShape = shape.GetAisShape();
	if(!hAisShape.IsNull())
	{
		m_hContext->Hilight(hAisShape, bUpdate);
	}
}

CSelectionSet* CRenderEngine::CreateSelectionSet(CAnyView& view)
{
	return new CSelectionSet(m_hContext, view.GetInternalView());
}

void CRenderEngine::UpdateGridPlane(const CSketchPlane& skplane)
{
	m_hViewMgr->SetPrivilegedPlane(skplane.GetPlane());
}